﻿$import( "SmitearWebGL.Core.ReferenceCounter" );
$import( "SmitearWebGL.Resources.Sampler" );
$import( "SmitearWebGL.Resources.Texture2D" );

SmitearWebGL.Resources.FrameBufferObject = SmitearWebGL.Core.ResourceItem.extend(
{
	/******** constructor ********/

	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Resources.FrameBufferObject" );
		
		/******** private property ********/

		this._renderSurface = null;
		this._renderTargets = null;
	}
	,
	
	/******** public method ********/
	
	getWidth : function()
	{
		return this._width;
	}
	,
	getHeight : function()
	{
		return this._height;
	}
	,
	getGlFrameBuffer : function()
	{
		if ( this._renderSurface )
			return this._renderSurface.getGlBuffer();
		else
			return null;
	}
	,
	getRenderTarget : function( usage )
	{
		return this._renderTargets[usage];
	}
	,
	createRenderSurface : function( width, height )
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		if ( width == undefined || height == undefined )
			$THROW( "帧缓冲对象大小无效" );
		
		this._width = width;
		this._height = height;
		
		this._renderSurface = this.context.resourceManager.createResource( this.name + "_redner_surface", SmitearWebGL.Resources.RenderSurface );
		this._renderSurface.createDepthSurface( this._width, this._height );
		this._renderSurface.addRef();
	}
	,
	createRenderTarget : function( usage, intformat, format, type, sampler )
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		var renderTarget = this.context.resourceManager.createResource( this.name + "RT" + usage, SmitearWebGL.Resources.Texture2D );
		renderTarget.usage = usage;
		renderTarget.createTexture( this._width, this._height, null, false,
		SmitearWebGL.Resources.Texture.Format.ARGB8,
		SmitearWebGL.Resources.Texture.Format.ARGB8,
		SmitearWebGL.Resources.Texture.Type.UBYTE, 0, 0 );
		renderTarget.bindSampler( sampler );
		
		this._renderTargets[usage] = renderTarget;
	}
	,
	clearRenderTarget : function( color, clearBit )
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		color = color || [0.0, 0.0, 0.0, 0.0];
		
		SmitearWebGL.Objects.Viewport.setClearColor( this.context.gl, color );
		SmitearWebGL.Objects.Viewport.clear( this.context.gl, clearBit );
	}
	,
	swapRenderTarget : function( usage1, usage2 )
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		var tmp = this._renderTargets[usage1];
		this._renderTargets[usage1] = this._renderTargets[usage2];
		this._renderTargets[usage2] = tmp;
	}
	,
	useRenderTarget : function( usage )
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		if ( this._renderSurface )
		{
			if ( this._renderSurface.isBinding() )
			{
				if ( usage == undefined || usage == -1 )
					return this._renderSurface.clearRenderTarget();
				else
					return this._renderSurface.bindRenderTarget( this._renderTargets[usage], 0 );
			}
			else
			{
				$THROW( false, "当前绑定的帧缓冲对象不是该实例" );
			}
		}
		
		return false;
	}
	,
	bind : function()
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		if ( this._renderSurface )
			this._renderSurface.bind();
	}
	,
	unbind : function()
	{
		$THROW( this.isResourceCreated(), "该资源还没有被创建." );
		
		if ( this._renderSurface )
			this._renderSurface.unbind();
	}
	,
	_createResource : function()
	{
		this._super();

		this._renderTargets = {};
	}
	,
	_destroyResource : function()
	{
		this._super();
		
		if ( this._renderSurface )
			this._renderSurface.release();
			
		for ( var i = 0; i < this._renderTargets.length; ++ i )
		{
			this._renderTargets[i].release();
			this._renderTargets[i] = null;
		}
		
		this._renderSurface = null;
		this._renderTargets = null;
	}
}
);